package com.taocyb.framework.remote.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http请求工具类
 */
public class HttpClientUtils {
	private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
	
	private static final String DEFAULT_CHARSET = "UTF-8";	//默认字符编码
	private static final int TIMEOUT_SECONDS = 5;			//默认超时时长(秒)
	
	/**
	 * HttpGet请求
	 * @param url 	   请求地址
	 */
	public static String doGet(String url){
		String content = null;
		
		HttpClient httpClient = new DefaultHttpClient(); 
		HttpGet httpGet = new HttpGet(url);
		try {
			content = httpClient.execute(httpGet, new BasicResponseHandler());
		} catch (ClientProtocolException e) {
			logger.error("该异常通常是协议错误导致,堆栈信息如下", e);
		} catch (IOException e) {
			logger.error("该异常通常是网络原因引起的,如HTTP服务器未启动等", e);
		} finally{
			httpGet.abort();
		}
		
		return content;
	}
	
	/**
	 * HttpPost请求 
	 * @param url		请求地址
	 * @param data		请求参数(如 type=1&status=1)
	 * @return
	 */
	public static String doPost(String url, String data){
		String content = null;
		String[] datas = data.split("&");
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(datas.length);
		for(String str : datas){
			nameValuePairs.add(new BasicNameValuePair(str.substring(0,str.indexOf("=")), str.substring(str.indexOf("=")+1)));
		}
		
		HttpClient httpClient = new DefaultHttpClient(); 
		HttpParams httpParams = httpClient.getParams();
		HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_SECONDS * 1000);
		
		HttpPost httpPost = new HttpPost(url);
		try {
			httpPost.setEntity(new StringEntity(URLEncodedUtils.format(nameValuePairs, DEFAULT_CHARSET)));
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
            if (null != entity) {
            	content = EntityUtils.toString(entity);
                EntityUtils.consume(entity);
            }
		} catch (UnsupportedEncodingException e) {
			logger.error("不支持的编码类型");
		} catch (ClientProtocolException e) {
			logger.error("该异常通常是协议错误导致,堆栈信息如下", e);
		} catch (IOException e) {
			logger.error("该异常通常是网络原因引起的,如HTTP服务器未启动等", e);
		} finally{
			httpPost.abort();
		}
		
		return content;
	}
}
